$(function(){

    function popFn(dataType , data){
        popHtml = ''
        popHtml += '<div id="graybg_pop" class="graybg_pop ie6fixed_t" style="display:block"></div>'
        popHtml += '<div class="col-lg-3 teacher_pop" style="display:boock">'
        popHtml += '    <span class="teacher_tit">添加</span>'
        popHtml += '    <i class="fa fa-times" aria-hidden="true"></i>'
        popHtml += '    <div class="form-group">'
        popHtml += '        <label>老师名称</label>'
        if(dataType == 1){
        popHtml += '        <input class="form-control txt_ipt" value="'+ data.name +'" placeholder="请输入老师名称">'
        }else{
        popHtml += '        <input class="form-control txt_ipt" value="" placeholder="请输入老师名称">'
        }
        popHtml += '    </div>'
        popHtml += '    <div class="form-group">'
        popHtml += '        <label>老师描述</label>'
        if(dataType == 1){
            popHtml += '        <textarea class="form-control text_ipt" rows="3"  placeholder="请输入老师描述">'+ data.description +'</textarea>'
        }else{
            popHtml += '        <textarea class="form-control text_ipt" rows="3"  placeholder="请输入老师描述"></textarea>'
        }
        popHtml += '    </div>'
        popHtml += '    <div class="big-photo">'
        popHtml += '        <div id="filePicker" style="float: left;margin-right: 10px;">'
        popHtml += '        </div>'
        popHtml += '        <div class="viewThumb">'
        if(dataType == 1){
            popHtml += '            <img id="imghead" border="0" src="'+ data.img_url +'" data_src = '+ data.img_url +' width="200" height="200" >'
        }else{
            popHtml += '            <img id="imghead" border="0" src="images/photo_icon.png" data_src = "images/photo_icon.png" width="200" height="200" >'
        }
        popHtml += '        </div>'
        popHtml += '        <input type="file" onchange="previewImage(this)" style="display: none;" id="previewImg">'
        popHtml += '    </div>'
        popHtml += '    <p class="p_btn"><button type="button" class="btn btn-lg btn-success ok_success" data_type="1">确定</button></p>'
        popHtml += '</div>'
    }
    //名师列表
    admin.ajaxFn(
        {
            url:admin.baseUrl.teacherlist,
            type : 'GET',
            data : {
                page_size : 10,
                page : 1,
                order_sn : '5745412',
                serial_number : '897845',
                status : '1'
            }
        },
        function(data){
            $('#teacher').html(template('teacherTpl',{'res': data}));
        }
    )

    //修改
    $('body').on( 'click' ,'.fa-pencil-square' , function(){
        var dataType = $(this).attr('data_type')
        var dataid = $(this).attr('data_id')
        admin.ajaxFn(
            {
                url:admin.baseUrl.teacherListMod,
                type : 'GET',
                data : {
                    id : dataid
                }
            },
            function(data){
                popFn(dataType , data  )
                $('body').append(popHtml)
                $('.teacher_tit').text('修改')
                $('.ok_success').attr({'data_type' : '1', 'data_id' : dataid})
                admin.webuploaderFn('点击上传', admin.baseUrl.teacherListUpdate, 'Images', 'gif,jpg,jpeg,png', 'image/gif,image/jpg,image/jpeg,image/png')
                admin.isloader = false;
            }
        )
    })

    //更新
    $('body').on( 'click' ,'.ok_success' , function(){
        var dataType = $(this).attr('data_type')
        var dataid = $(this).attr('data_id')
        if($('.txt_ipt').val().length <= 0){
            layer.msg('请输入名称')
            return false
        }else if($('.text_ipt').val().length <= 0){
            layer.msg('请输入描述')
            return false
        }else if(admin.isloader) {
              layer.msg('请选择图片')
              return false
        }else{
            if(dataType == 1){

                admin.uploader.options.formData.token = admin.getCookierl('cookval');
                admin.uploader.options.formData.name_t = $('.txt_ipt').val();
                admin.uploader.options.formData.description = $('.text_ipt').val();
                admin.uploader.options.formData.id_t = dataid
                admin.uploader.on('uploadSuccess', function (file, response) {
                    admin.ajaxFn(
                        {
                            url: admin.baseUrl.teacherlist,
                            type: 'GET',
                            data: {
                                page_size: 20,
                                page: 1,
                                order_sn : '5745412',
                                serial_number : '897845',
                                status : '1'
                            }
                        },
                        function (data) {
                            $('#graybg_pop , .teacher_pop').remove();
                            admin.isloader = true;
                            layer.msg('修改成功')
                            $('#teacherTpl').html(template('teacherTplTpl', {'res': data}));
                        }
                    )
                });
                admin.uploader.on('uploadComplete', function (file) {
                });
                if (admin.uploader.state === 'ready') {
                    admin.uploader.upload();
                } else if (admin.uploader.state === 'paused') {
                    admin.uploader.upload();
                } else if (admin.uploader.state === 'uploading') {
                    admin.uploader.stop();
                }
            }else{
                admin.uploader.options.formData.token = admin.getCookierl('cookval');
                admin.uploader.options.formData.name_t = $('.txt_ipt').val();
                admin.uploader.options.formData.description = $('.text_ipt').val();
                admin.uploader.on('uploadSuccess', function (file, response) {
                    admin.ajaxFn(
                        {
                            url: admin.baseUrl.teacherlist,
                            type: 'GET',
                            data: {
                                page_size: 20,
                                page: 1,
                                order_sn : '5745412',
                                serial_number : '897845',
                                status : '1'
                            }
                        },
                        function (data) {
                            $('#graybg_pop , .teacher_pop').remove();
                            admin.isloader = true;
                            layer.msg('添加成功')
                            $('#teacher').html(template('teacherTpl', {'res': data}));
                        }
                    )
                });
                admin.uploader.on('uploadComplete', function (file) {
                });
                if (admin.uploader.state === 'ready') {
                    admin.uploader.upload();
                } else if (admin.uploader.state === 'paused') {
                    admin.uploader.upload();
                } else if (admin.uploader.state === 'uploading') {
                    admin.uploader.stop();
                }
            }
        }
    })



    //增加
    $('body').on( 'click' ,'.add_list' , function(){
        var dataType = $(this).attr('data_type')
        var dataid = $(this).attr('data_id')
        popFn(dataType , '')
        $('body').append(popHtml)
        $('.teacher_tit').text('添加')
        $('.ok_success').attr({'data_type' : '2', 'data_id' : dataid})
        admin.webuploaderFn('点击上传', admin.baseUrl.teacherListAdd, 'Images', 'gif,jpg,jpeg,png', 'image/gif,image/jpg,image/jpeg,image/png')
    })
    //删除
    $('body').on( 'click' ,'.fa-minus-square' , function(){
        var dataid = $(this).attr('data_id')
        layer.open({
            type: 1
            ,title: false //不显示标题栏
            ,closeBtn: false
            ,area: '300px;'
            ,shade: 0.8
            ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
            ,btn: ['确定', '取消']
            ,btnAlign: 'c'
            ,moveType: 1 //拖拽模式，0或者1
            ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">确定要删除吗？</div>'
            ,yes: function(){
                $('.layui-layer-shade, .layui-layer').remove();
                admin.ajaxFn(
                    {
                        url:admin.baseUrl.teacherListRemove,
                        type : 'GET',
                        data : {
                            id : dataid
                        }
                    },
                    function(data){
                        layer.msg('删除成功！')
                        //名师列表
                        admin.ajaxFn(
                            {
                                url:admin.baseUrl.teacherlist,
                                type : 'GET',
                                data : {
                                    page_size : 10,
                                    page : 1,
                                    order_sn : '5745412',
                                    serial_number : '897845',
                                    status : '1'
                                }
                            },
                            function(data){
                                $('#teacher').html(template('teacherTpl',{'res': data}));
                            }
                        )
                    }
                )
            }

        });

    })



})

